package com.itheima.edu.info.maneger.controller;

import com.itheima.edu.info.maneger.domain.Teacher;
import com.itheima.edu.info.maneger.factory.SystemFactory;
import com.itheima.edu.info.maneger.service.TeacherService;

import java.util.Scanner;

public abstract class BaseTeacherController {
    //2. 获取用户的选择,用Scanner实现键盘录入数据

    Scanner sc = new Scanner(System.in);
    //TeacherService teacherService = new TeacherService();
    TeacherService teacherService = SystemFactory.getTeacherService();

    public final void start() {
        loop:
        while (true) {
            //1. 展示欢迎页面,用输出语句完成主界面的编写
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choice = sc.next();
            //3. 根据用户的选择执行对应的操作,用switch语句完成操作的选择
            switch (choice) {
                case "1":
                    //System.out.println("添加学生");
                    addTeacher();
                    break;
                case "2":
                    //System.out.println("删除学生");
                    deleteTeacherById();
                    break;
                case "3":
                    //System.out.println("修改学生");
                    updateTeacher();
                    break;
                case "4":
                    //System.out.println("查看学生");
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("谢谢使用");
                    break loop;
                default:
                    System.out.println("输入有误，重新输入");
                    break;

            }
        }
    }

    //修改学生
    public final void updateTeacher() {
        Teacher[] stu = teacherService.findAllTeacher();
        if (stu == null){
            System.out.println("没有信息，请先添加信息");
            return;
        }
        String updateId = inputTeacherId();
        Teacher newteacher = inputTeacherInfo(updateId);
        //调用teacherService的updateTeacher方法，传入参数
        teacherService.updateTeacher(updateId, newteacher);

        System.out.println("修改成功！");

    }


    //删除学生方法
    public final void deleteTeacherById() {
        Teacher[] stu = teacherService.findAllTeacher();
        if (stu == null){
            System.out.println("请先添加信息");
            return;
        }
        String id = inputTeacherId();
        teacherService.deleteTeacher(id);
        System.out.println("删除成功！");
    }

    //获取ID
    public final String inputTeacherId() {
        String id;
        while (true) {
            System.out.println("请输入学生的学号");
            id = sc.next();
            boolean flag = teacherService.isExists(id);
            if (flag) {
                break;
            } else {
                System.out.println("无此学号，重新输入");

            }
        }
        return id;
    }

    //查看学生方法
    public final void findAllTeacher() {
        //得到学生数组
        Teacher[] stu = teacherService.findAllTeacher();
        //判断数组的内存地址是否为空
        if (stu == null) {
            System.out.println("查无信息，请先添加信息");
            return;
        }
        System.out.println("学号\t\t姓名\t\t年龄\t生日");
        //遍历数组，获取学生信息并打印
        for (int i = 0; i < stu.length; i++) {
            if (stu[i] != null) {
                Teacher teacher = stu[i];
                System.out.println(teacher.getId() + "\t" + "\t" + teacher.getName() + "\t" + "\t" + teacher.getAge() + "\t\t" + teacher.getBirthday());
            }
        }

    }

    //添加学生
    public final void addTeacher() {
        Teacher teacher = new Teacher();
        String id;
        while (true) {
            System.out.println("请输入学号");
            //键盘接受学生ID
            id = sc.next();
            //判断此ID是否存在
            boolean flag = teacherService.isExists(id);
            if (flag) {
                System.out.println("此学号已经存在，请重新输入");
            } else {
                break;
            }
        }
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        int age = sc.nextInt();
        System.out.println("请输入生日");
        String birthday = sc.next();
        //将用户输入的数据进行封装
        teacher.setId(id);
        teacher.setName(name);
        teacher.setAge(age);
        teacher.setBirthday(birthday);

        boolean flag = teacherService.addTeacher(teacher);
        if (flag) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }

    //封装信息
    public abstract Teacher inputTeacherInfo(String updateId);
}
